package com.swc.consumer.controller;

import com.netflix.ribbon.Ribbon;
import com.swc.consumer.client.UserClient;
import com.swc.consumer.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * @author songwc
 * @date 2020/5/30 12:14
 */
@RestController
//@DefaultProperties(defaultFallback = "defaultFallback")
@RequestMapping("consumer")
public class ConsumerController {


    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;// Eureka客户端，可以获取到服务实例信息

//    @Autowired
//    private RibbonLoadBalancerClient client;

    @Autowired
    private UserClient userClient;

    @GetMapping("{id}")
    public User queryById(@PathVariable("id") Long id){
        return userClient.queryUserById(id);
    }

 /*   @GetMapping("{id}")
//    @HystrixCommand(commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "5000")
//    })
    @HystrixCommand(commandProperties = {
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"),
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60")
    })
    //@HystrixCommand
    public String queryById(@PathVariable("id") Long id){
        if (id % 2 == 0){
            throw  new RuntimeException("111");
        }
        String baseUrl = "http://user-service/user/" + id;
        String user  = restTemplate.getForObject(baseUrl, String.class);
        return user;
    }*/

    public String defaultFallback(){
        return "服务器繁忙";
    }

    @GetMapping
    public List<User> consume(@RequestParam("ids") List<Long> ids) {
        List<User> users = new ArrayList<>();
        // String baseUrl = "http://localhost:8081/user/";
        // 根据服务名称，获取服务实例
      //  List<ServiceInstance> instances = discoveryClient.getInstances("user-service");
        // 因为只有一个UserService,因此我们直接get(0)获取
       // ServiceInstance instance = instances.get(0);
   //     ServiceInstance instance = client.choose("user-service");
        // 获取ip和端口信息
    //    String baseUrl = "http://"+instance.getHost() + ":" + instance.getPort()+"/user/";
        String baseUrl = "http://user-service/user/";
        ids.forEach(id -> {
            // 我们测试多次查询，
            users.add(this.restTemplate.getForObject(baseUrl + id, User.class));
            // 每次间隔500毫秒
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        return users;
    }
}

